import java.util.List;
import java.util.Map;
import javax.xml.ws.BindingProvider;

import librarymanager.BookDAO;
import librarymanager.CustomerDAO;
import librarymanager.LibraryManager;
import librarymanager.LibraryManagerService;

import org.jboss.ws.core.StubExt;

public class Static2
{
	public static void main(String[] arg) throws Exception
	{
		LibraryManagerService service = new LibraryManagerService();
		LibraryManager manager = service.getLibraryManagerPort();
		
		System.out.println("manager="+manager+"\n");

		BindingProvider bindingProvider = (BindingProvider)manager;

		Map<String, Object> map = bindingProvider.getRequestContext();
		map.put(StubExt.PROPERTY_AUTH_TYPE, StubExt.PROPERTY_AUTH_TYPE_BASIC);
		
		map = login(map, "henk", "geheim");

		printBooks(manager); 
		printCustomers(manager);
		manager.addCredits("Eric", 1);
		printCustomers(manager);
		
		login(map, "john", "geheim");
		
		printBooks(manager); 
		manager.newBook("aapje");
		printBooks(manager);   

		printCustomers(manager); 
		manager.newCustomer("aapje");
		printCustomers(manager);
		manager.removeCustomer("aapje");    
		printCustomers(manager);
		
		login(map, "fred", "geheim");
		
		printCustomers(manager);
		manager.lend("aapje", "Eric");
		printCustomers(manager);
		
		login(map, "john", "geheim");
		
		printBooks(manager);
		manager.removeBook("aapje");
		printBooks(manager);		
	}
	
	private static void printBooks(LibraryManager manager)
	{
		List<BookDAO> books = manager.allBook();
		
		for (BookDAO book : books)
		{
			System.out.println(book.getTitle()); 
		}     
		System.out.println("----------------------------------------------");
	}
	
	private static void printCustomers(LibraryManager manager)
	{
		List<CustomerDAO> customers = manager.allCustomers();
		for (CustomerDAO customer : customers)
		{
			System.out.print(customer.getName()+", "+customer.getCredits());
			List<String> books = customer.getBooks();
			for (String title : books)
			{
				System.out.print(", "+title); 
			}
			System.out.println();
		} 
		System.out.println("----------------------------------------------");   
	}
	
	private static Map<String, Object> login(Map<String, Object> map, String username, String password) {
		map.put(BindingProvider.USERNAME_PROPERTY, username);
		map.put(BindingProvider.PASSWORD_PROPERTY, password);
		
		return map;
	}
}